package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.TbCards;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbPackage;
import com.ruoyi.system.service.ITbPackageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 卡套餐管理Controller
 * 
 * @author ruoyi
 * @date 2023-03-04
 */
@RestController
@RequestMapping("/system/package")
public class TbPackageController extends BaseController
{
    @Autowired
    private ITbPackageService tbPackageService;

    /**
     * 查询卡套餐管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:package:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbPackage tbPackage)
    {
        startPage();
        List<TbPackage> list = tbPackageService.selectTbPackageList(tbPackage);
        list.stream().forEach(d->{if("0".equals(d.getPackageSize())){d.setPackageSize("不限量");}});
        return getDataTable(list);
    }

    /**
     * 查询绑定套餐
     */
    @PreAuthorize("@ss.hasPermi('system:package:query')")
    @GetMapping(value = "/withCard")
    public TableDataInfo withCardList(TbPackage tbPackage)
    {
        startPage();
        logger.info("cardId:{},bangDing:{}",tbPackage.getCardId(),tbPackage.getBangDing());
        return getDataTable(tbPackageService.withCardList(tbPackage));
    }

    /**
     * 绑定卡套餐
     */
    @PreAuthorize("@ss.hasPermi('system:package:query')")
    @PostMapping(value = "/setCardAndPackages")
    public AjaxResult setCardAndPackages(@RequestBody JSONObject jsonObject)
    {
        logger.info("jsonObject:{}",jsonObject);
        return success(tbPackageService.setCardAndPackages(jsonObject));
    }


    /**
     * 解除绑定卡套餐
     */
    @PreAuthorize("@ss.hasPermi('system:package:query')")
    @PostMapping(value = "/setCardAndUnPackages")
    public AjaxResult setCardAndUnPackages(@RequestBody JSONObject jsonObject)
    {
        logger.info("jsonObject:{}",jsonObject);
        return success(tbPackageService.setCardAndUnPackages(jsonObject));
    }

    /**
     * 导出卡套餐管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:package:export')")
    @Log(title = "卡套餐管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbPackage tbPackage)
    {
        List<TbPackage> list = tbPackageService.selectTbPackageList(tbPackage);
        ExcelUtil<TbPackage> util = new ExcelUtil<TbPackage>(TbPackage.class);
        util.exportExcel(response, list, "卡套餐管理数据");
    }

    /**
     * 获取卡套餐管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:package:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbPackageService.selectTbPackageById(id));
    }

    /**
     * 新增卡套餐管理
     */
    @PreAuthorize("@ss.hasPermi('system:package:add')")
    @Log(title = "卡套餐管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPackage tbPackage)
    {
        return toAjax(tbPackageService.insertTbPackage(tbPackage));
    }

    /**
     * 修改卡套餐管理
     */
    @PreAuthorize("@ss.hasPermi('system:package:edit')")
    @Log(title = "卡套餐管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPackage tbPackage)
    {
        return toAjax(tbPackageService.updateTbPackage(tbPackage));
    }

    /**
     * 删除卡套餐管理
     */
    @PreAuthorize("@ss.hasPermi('system:package:remove')")
    @Log(title = "卡套餐管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbPackageService.deleteTbPackageByIds(ids));
    }
}
